<?php
class Z1chucvusvsController extends AppController {

	var $name = 'Z1chucvusvs';
    var $components = array('RequestHandler');
    var $uses = array('Z1chucvusv','Chidoansv','Khoahoc','Chucv','Tgiachidoan','Sinhvien','Chucvusv');
    
	function index($chucv_id=0,$khoahoc_id=0,$chidoan_id=0) {
		$isAjax = 0;		
		if($this->RequestHandler->isAjax())
		{
            $isAjax = 1;
			$this->layout='ajax';
			$chucv_id =$this->data['Z1chucvusvs']['chucvu'];
			if($chucv_id=='')
			{
				$this->Z1chucvusv->recursive = 0;
            	$this->set('z1chucvusvs', $this->paginate(array('Chidoansv.khoa_id'=>$this->Session->read('khoa_id'),
                                                            'ngayketthuc'=>NULL)));
			}
			else
			{
				$this->Z1chucvusv->recursive = 0;
            	$this->set('z1chucvusvs', $this->paginate(array('Chidoansv.khoa_id'=>$this->Session->read('khoa_id'),
                                                            'ngayketthuc'=>NULL,'chucv_id'=>$chucv_id)));
			}
        }else{
            $this->Z1chucvusv->recursive = 0;
            $this->set('z1chucvusvs', $this->paginate(array('Chidoansv.khoa_id'=>$this->Session->read('khoa_id'),
                                                            'ngayketthuc'=>NULL)));
        }
        $chucvu=$this->Chucv->find('list',array('fields'=>array('tenchucvu')));
        /**
         * Them chức vụ cho sinh viên
         */
        $khoahoc=$this->Khoahoc->find('list',array('fields'=>array('nambatdau')));
        $chidoan=$this->Chidoansv->find('list',array('conditions'=>array('khoahoc_id'=>$khoahoc_id,'khoa_id'=>$this->Session->read('khoa_id')),
                                                    'fields'=>array('tenchidoan')));
                
        $tenkhoa=$this->Z1chucvusv->query("select tenkhoa from khoas where id=".$this->Session->read('khoa_id'));
        $this->set(compact('tenkhoa','chucvu','chucv_id','isAjax','khoahoc','chidoan','khoahoc_id','chidoan_id'));
	}
    
    function laycd($chidoan_id=0){
        $khoahoc_id =$this->data['Them']['khoahoc'];
        $chidoan=$this->Chidoansv->find('list',array('conditions'=>array('khoahoc_id'=>$khoahoc_id,'khoa_id'=>$this->Session->read('khoa_id')),
                                                    'fields'=>array('chidoan')));
        $this->set(compact('chidoan','chidoan_id'));
    }
    
    function laysv($sinhvien_id=0){
        $chidoan_id =$this->data['chidoan'];
        $sinhviens=$this->Tgiachidoan->find('all',array('ngaychuyen'=>NULL,'conditions'=>array('chidoansv_id'=>$chidoan_id),
        													'recursive'=>0));
        $sinhvien =array(); 
        foreach ($sinhviens as $sv)
        {
        	$sinhvien[$sv['Sinhvien']['id']] = $sv['Sinhvien']['name'];
        }
        
        $this->set(compact('sinhvien','sinhvien_id'));
    }
    
    function luu (){
        If(!empty($this->data)){
            $this->Chucvusv->create();
                $record['Chucvusv']['chucv_id']=$this->data['Them']['chucvu'];
                $record['Chucvusv']['sinhvien_id']=$this->data['sinhvien'];
                $record['Chucvusv']['ngaybatdau']=date('Y-m-d');
        if ($this->Chucvusv->save($record)) {
				$this->Session->setFlash(__('Thông tin đã lưu', true),'success');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Thông tin không thể lưu.', true),'error');
			}
        }
    }
	
    function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid z1chucvusv', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('z1chucvusv', $this->Z1chucvusv->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Z1chucvusv->create();
			if ($this->Z1chucvusv->save($this->data)) {
				$this->Session->setFlash(__('The z1chucvusv has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The z1chucvusv could not be saved. Please, try again.', true));
			}
		}
		$chucvs = $this->Z1chucvusv->Chucv->find('list');
		$sinhviens = $this->Z1chucvusv->Sinhvien->find('list');
		$chidoansvs = $this->Z1chucvusv->Chidoansv->find('list');
		$this->set(compact('chucvs', 'sinhviens', 'chidoansvs'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid z1chucvusv', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Z1chucvusv->save($this->data)) {
				$this->Session->setFlash(__('The z1chucvusv has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The z1chucvusv could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Z1chucvusv->read(null, $id);
		}
		$chucvs = $this->Z1chucvusv->Chucv->find('list');
		$sinhviens = $this->Z1chucvusv->Sinhvien->find('list');
		$chidoansvs = $this->Z1chucvusv->Chidoansv->find('list');
		$this->set(compact('chucvs', 'sinhviens', 'chidoansvs'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for z1chucvusv', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Z1chucvusv->delete($id)) {
			$this->Session->setFlash(__('Z1chucvusv deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Z1chucvusv was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
